package com.heliang.ui.info;

import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appxiehui2.R;
import com.heliang.common.StringUtils;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.util.Constants;

public class InfoDetailActivity extends Activity {

    private Intent intent;

    private InfoBean infoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 设置只能竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(R.layout.info_detail);

        // 设置不自动弹出键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        intent = getIntent();
        infoBean = (InfoBean) intent.getSerializableExtra("infoBean");
        if (infoBean != null) {
            initView();
        }

        Toast.makeText(getApplicationContext(), infoBean.getCompanyName(), Toast.LENGTH_SHORT).show();
    }

    /**
     * @Description: 初始化元素
     * @param:
     * @return: void
     * @throws
     * @author heliang
     * @Date 2013-10-19 下午3:50:12
     */
    public void initView() {
        String timeStr = "";
        if (infoBean.getCreated() != 0) {
            timeStr = String.valueOf(StringUtils.TimeStamp2Date("yyyy-MM-dd", infoBean.getCreated()));
        }

        /* 设置评论数据以及点击事件 */
        String commentStr = "";
        TextView comentCount = (TextView) findViewById(R.id.comment_edit);
        commentStr = getString(R.string.comment) + infoBean.getCommentCount();
        comentCount.setText(commentStr);

        /* 设置标题 */
        TextView titleView = (TextView) findViewById(R.id.info_detail_title);
        titleView.setText(infoBean.getTitle());
        TextPaint tp = titleView.getPaint();
        tp.setFakeBoldText(true); // 设置粗体

        /* 设置时间和来源 */
        TextView sourceView = (TextView) findViewById(R.id.info_detail_comefrom);
        sourceView.setText(timeStr + getString(R.string.come_from) + " " + infoBean.getCompanyName());
        
        /* 设置资讯图片 */
        ImageView infoDetailImageview = (ImageView) findViewById(R.id.info_detail_imageview);
        if (infoBean.getPic1Url() != null && !infoBean.getPic1Url().equals("")) {
           // infoDetailImageview.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.detail_default_pic));
            FinalBitmap finalBitMap =  FinalBitmap.create(this);
            finalBitMap.configLoadingImage(R.drawable.detail_default_pic);
            finalBitMap.configBitmapMaxHeight(308);
            finalBitMap.configBitmapMaxWidth(420);
            finalBitMap.display(infoDetailImageview, infoBean.getPic1Url());
           
           
        }
        
        /* 设置内容 */
        TextView infoContent = (TextView) findViewById(R.id.info_detail_content);
        infoContent.setText(infoBean.getContent());
        
        // 为返回按钮设置事件
        ImageView backImageView = (ImageView) findViewById(R.id.info_back_btn);
        backImageView.setOnClickListener(onclickListener);

    }
    
    /**
     * 点击事件
     */
    private OnClickListener onclickListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
           switch (v.getId()) {
            case R.id.info_back_btn:
                setResult(Constants.HOT_INFO, intent);
                finish();
                break;

            default:
                break;
        }
            
        }
    };
}
